package com.common.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.component.Pagination;
import com.common.manager.OrgInfoManager;
import com.common.model.OrgInfoModel;
import com.common.model.StaffInfoModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/org")
public class OrgInfoController extends BaseController{

	 private static String modelUrl="/management/org/";
	 
    @Autowired
	private OrgInfoManager orgInfoManager;

    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("/pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("orgInfoModel") OrgInfoModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<DataMap> dataList = orgInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("orgList", dataList);
		return modelView;
	}
	
	
	/**
	 * 功能：初始化添加页面
	 * @param orgInfoModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("orgInfoModel") OrgInfoModel orgInfoModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		modelView.addObject("vo",orgInfoModel);
		return modelView;
	}
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		OrgInfoModel orgInfoModel = (OrgInfoModel)orgInfoManager.getObjectInfo(dataMap);
		modelView.addObject("vo",orgInfoModel);
		return modelView;
	}
	
	
	/**
	 * 功能：执行插入操作
	 * @param orgInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public Object insert(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("orgInfoModel") OrgInfoModel orgInfoModel) {
		try{
			int opNum = orgInfoManager.insertObjectAndGetID(orgInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param orgInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public Object update(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("orgInfoModel") OrgInfoModel orgInfoModel) {
		try{
			int opNum =orgInfoManager.updateObject(orgInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/{intID}/delete")
	public Object delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		try{
			IData dataMap = new DataMap();
			dataMap.put("intID", intID);
			int opNum =orgInfoManager.deleteObject(dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：执行批量删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteAll")
	public Object deleteAll(HttpServletResponse response,HttpServletRequest request) {
		String ids = request.getParameter("ids");
		try{
			IData dataMap = new DataMap();
			dataMap.put("ids", ids);
			int opNum =orgInfoManager.deleteObject("deleteAllObject",dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 角色数据权限机构树
	 * @param response
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/loadRoleOrgTree")
	public void loadRoleOrgTree(HttpServletResponse response,HttpServletRequest request){

		String roleId = request.getParameter("roleId");
		String menuFunc = orgInfoManager.getRoleOrgTreeList(roleId);
		this.ajaxJson(response, menuFunc);
	}
	
	
	/**
	 * 功能：检查机构编码是否正常
	 * @param response
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/checkOrgCode")
	public void checkOrgCode(HttpServletResponse response,HttpServletRequest request){
		String orgCode = request.getParameter("orgCode");
		String porgCode = request.getParameter("porgCode");
		boolean isFlag= true;
		JSONObject rtnMsg = new JSONObject();
		if(orgCode!=null&&porgCode!=null){
			if(orgCode.equals(porgCode)){
				isFlag =false;
				rtnMsg.put("msg", "机构编码和父机构编码一致");
			}else if(!orgCode.startsWith(porgCode)){
				isFlag =false;
				rtnMsg.put("msg", "机构编码应该以父机构编码开头");
			}else{
				try {
					isFlag =  orgInfoManager.checkOrgCode(orgCode);
					if(!isFlag){
						rtnMsg.put("msg", "机构编码已存在请重新输入");
					}else{
						rtnMsg.put("msg", "校验通过");
					}
				} catch (Exception e) {
					isFlag =false;
					rtnMsg.put("msg", "校验发生异常");
				}
			}
		}else{
			rtnMsg.put("msg", "机构编码不能为空");
		}
		rtnMsg.put("isFlag", isFlag);
		this.ajaxJson(response, rtnMsg.toString());
	}
	
	
	/**
	 * 功能：机构管理机构树
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loadOrgTree")
	public Object loadOrgTree(HttpServletRequest request,HttpServletResponse response){
		StaffInfoModel  staffInfoModel = super.getUserSession(request);
		String orgCode = staffInfoModel.getOrgCode();
		String reponseJson ="";
		try {
			 reponseJson = orgInfoManager.loadOrgTree(getSessionUserInfo(),"management/org/pageList.do","ORG");
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info(reponseJson);
		return super.ajaxJson(response,reponseJson);
	}
	
	
	
	/**
	 * 功能：人员机构树
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loadStaffOrgTree")
	public Object loadStaffOrgTree(HttpServletRequest request,HttpServletResponse response){
		StaffInfoModel  staffInfoModel = super.getUserSession(request);
		String orgCode = staffInfoModel.getOrgCode();
		String reponseJson ="";
		try {
			 reponseJson = orgInfoManager.loadOrgTree(getSessionUserInfo(),"management/staff/pageList.do","STAFF");
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info(reponseJson);
		return super.ajaxJson(response,reponseJson);
	}
	
}